package top.rainf.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 星环
 * @date 2023/5/9 20:35:30
 * @description 配置跨域拦截器
 */

@Configuration
public class CorsConfig implements WebMvcConfigurer {


    /**
     *  对所有访问路径添加拦截器
     * @param registry 拦截器注册
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/**");
        //WebMvcConfigurer.super.addInterceptors(registry);
    }

    /**
     * 添加跨域访问映射。。。
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //对于所有的访问，允许以下域名的跨域请求
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8848","http://127.0.0.1:8848"
                        ,"http://127.0.0.1:5500","http://localhost:5500","http://localhost:8080")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE")
                .allowCredentials(true);

    }
}
